package com.nth.rooster.authentication.controllers;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nth.rooster.authentication.beans.LoginBean;
import com.nth.rooster.authentication.beans.OrganisationInfoBean;
import com.nth.rooster.authentication.services.AuthenticationServiceI;
import com.nth.rooster.core.exception.NErrorCode;
import com.nth.rooster.core.exception.RoosterCoreException;
import com.nth.rooster.core.pojo.AuthenticationBean;

@Controller
@RequestMapping("/Auth")
public class Login {
	
	static Logger LOGGER = Logger.getLogger(Login.class);

	@Autowired
	AuthenticationServiceI authenticationServiceI;

	@RequestMapping(value = "/validateURL", method = RequestMethod.GET)
	@ResponseBody
	public OrganisationInfoBean validateURL() {
		LOGGER.info("Enter Login Class of validateURL()");
		OrganisationInfoBean organisationInfoBean = authenticationServiceI
				.validateURL();
		LOGGER.info("Exited Login Class of validateURL()");
		return organisationInfoBean;
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@ResponseBody
	public AuthenticationBean login(@RequestBody LoginBean loginBean) {
		LOGGER.info("Enter Login Class of login()");
		AuthenticationBean authenticationBean = null;
		try {
			 authenticationBean = authenticationServiceI
					.login(loginBean);
		} catch (Exception e) {
			throw new RoosterCoreException(NErrorCode.INVALID_LOGIN_AUTHENTICATION,loginBean.getUsername() );
		}
		LOGGER.info("Exited Login Class of login()");
		return authenticationBean;
	}
	
	@RequestMapping(value = "/initialSignin", method = RequestMethod.POST)
	@ResponseBody
	public AuthenticationBean validateSuperUserInitialSignIn(@RequestBody LoginBean loginBean) {
		LOGGER.info("Entered::Authentication::Login::validateSuperUserInitialSignIn()");
		AuthenticationBean authenticationBean = authenticationServiceI.validateSuperUserInitialSignIn(loginBean);
		LOGGER.info("Exit::Authentication::Login::validateSuperUserInitialSignIn()");
		return authenticationBean;
	}

	public AuthenticationServiceI getAuthenticationServiceI() {
		return authenticationServiceI;
	}

	public void setAuthenticationServiceI(
			AuthenticationServiceI authenticationServiceI) {
		this.authenticationServiceI = authenticationServiceI;
	}

}
